package com.hxzy.research.controller.usermanager;

import com.hxzy.research.pojo.TblClass;
import com.hxzy.research.service.pageandmanager.AddClassManager;
import com.hxzy.research.service.pageandmanager.FrontTeach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ClassController {
    /**
     *分公司班级
     */
    @Autowired private FrontTeach frontTeach;
    @Autowired private AddClassManager addClassManager;
    @RequestMapping("/user/manager/forwardClassManager.go")
    public String forwardClassManager(Model model, Integer branchid, HttpSession session)throws Exception{
        session.setAttribute("branchid",branchid);

        //查询班级
        List<TblClass> classList=frontTeach.findClassByid(branchid);
        model.addAttribute("tclassList",classList);
        return "admin/manager/class/classManager";
    }

    //跳转到班级添加页面
    @RequestMapping("/user/manager/forwardAddTclass.go")
    public String forwardAddTclass()throws Exception{
        return "admin/manager/class/addClass";
    }
    //班级添加页面
    @RequestMapping("/user/manager/addTclass.go")
    public @ResponseBody TblClass addTclass(TblClass tblClass,HttpSession session)throws Exception{
        //执行添加
        TblClass tc=new TblClass();
        tc.setBranchid((Integer) session.getAttribute("branchid"));
        tc.setClassname(tblClass.getClassname());
        addClassManager.addClass(tc);
        return tblClass;
    }

    //跳转到课程修改页面
    @RequestMapping("/user/manager/forwardUpdateTclass.go")
    public String forwardUpdateTclass(Model model,Integer cid,HttpSession session)throws Exception{
        session.setAttribute("cid",cid);
        //通过课程id查课程
        TblClass tblClass=addClassManager.findClassById(cid);
        model.addAttribute("tclass",tblClass);
        return "admin/manager/class/updateClass";
    }

    //班级修改页面
    @RequestMapping("/user/manager/updateTClass.go")
    public @ResponseBody TblClass updateTClass(TblClass tblClass,HttpSession session) throws Exception{
        //执行修改
        TblClass tc=new TblClass();
        tc.setId((Integer) session.getAttribute("cid"));
        tc.setBranchid((Integer) session.getAttribute("branchid"));
        tc.setClassname(tblClass.getClassname());
        addClassManager.updateClass(tc);
        return tblClass;
    }

    //班级删除
    @RequestMapping("/user/manager/deleteTclass.go")
    public String deleteTclass(Integer cid,HttpSession session)throws Exception{
        //执行删除
        addClassManager.deleteClass(cid);
        Integer branchid= (Integer) session.getAttribute("branchid");
        return "redirect:/user/manager/forwardClassManager.go?branchid="+branchid;
    }
}
